package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class CategoryServiceImpl implements CategoryService {

    private final CategoryMapper categoryMapper;

    private final SetmealMapper setmealMapper;

    private final DishMapper dishMapper;

    @Override
    public void addCategory(CategoryDTO categoryDTO) {
        Category category = BeanUtil.copyProperties(categoryDTO, Category.class);

        category.setStatus(StatusConstant.ENABLE);

        // 这个过于冗余了，每个修改还有插入都有需要进行改进
        //category.setCreateTime(LocalDateTime.now());
        //category.setUpdateTime(LocalDateTime.now());
        //category.setCreateUser(BaseContext.getCurrentId());
        //category.setUpdateUser(BaseContext.getCurrentId());

        categoryMapper.addCategory(category);
    }

    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());

        Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO);


        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void status(Long id, Integer status) {
        Category category = Category.builder()
                .status(status)
                .id(id)
                //.updateTime(LocalDateTime.now())
                //.updateUser(BaseContext.getCurrentId())
                .build();
        categoryMapper.update(category);
    }

    @Override
    public void delete(Long id) {
        Category category = Category.builder()
                .id(id)
                .build();

        List<Setmeal> setmealList = setmealMapper.getByCategoryId(category);
        if(setmealList!=null&& !setmealList.isEmpty()){
            throw new DeletionNotAllowedException(MessageConstant.
                    CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        List<DishVO> dishVOList = dishMapper.getByCategoryId(category);
        if(dishVOList!=null&& !dishVOList.isEmpty()){
            throw new DeletionNotAllowedException(MessageConstant.
                    CATEGORY_BE_RELATED_BY_DISH);
        }
        categoryMapper.delete(id);
    }

    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = BeanUtil.copyProperties(categoryDTO, Category.class);
        //category.setUpdateTime(LocalDateTime.now());
        //category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.update(category);
    }

    @Override
    public List<Category> getByType(Category category) {
        List<Category>  categoryList=categoryMapper.getByType(category);
        return categoryList;
    }
}
